-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize the ToHexString
#3566
Optimize the ToHexString
#3566
Conversation
@@ -17,7 +17,7 @@ namespace Neo.Extensions | |||
{ | |||
public static class ByteExtensions | |||
{ | |||
private static readonly char[] s_hexChars = "0123456789abcdef".ToCharArray(); | |||
private const string s_hexChars = "0123456789abcdef"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can be const string faster than char string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If access to the char of a string is faster than access to the char array, the benchmark was wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If access to the char of a string is faster than access to the char array, the benchmark was wrong
Const value may more friendly for compiler and JIT optimization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If access to the char of a string is faster than access to the char array, the benchmark was wrong
UTF-8 string is slower, but this string just asc-ii, so it can be optimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong benchmark, i'm sure
Why this is slower, there are just two operations: an if
check and a pointer add(a lea
instruction on x86), and it has an Intrinsic
attribute, so JIT can optimize it.
There are also a bound check(for IndexOutOfRangeException
) and a pointer computation on array access.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#dotnet_jit-and-dotnet_gc
https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/jit/investigate-stress.md#jit-stress-debug-builds-only--debug-and-checked
This is the one you want
https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/jit/viewing-jit-dumps.md#disassembly-output
enable this on your computer and run dotnet in commandline to test JIT and GC outputs
DOTNET_JitDisasm={method-list} - output disassembly for the specified functions. E.g., DOTNET_JitDisasm=Main, DOTNET_JitDisasm=Main Test1 Test2, DOTNET_JitDisasm=* (for all functions).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like your analysis.
I need 2 days to benchmark as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer char array, it will be faster, but if all @neo-project/core is ok, i'm ok
I tested again.
|
this always will be slower than direct array access, but it's ok, just a ms |
Description
I tested some RPC interfaces and found that GC consumes a quite bit of CPU.
And I found some memory allocations that can be optimized.
For example:
Th current impl of
ToHexString
:StringBuilder
can be pre-allocated.AppendFormat
will box the byteb
and causes a/some memory allocation(s).Perf tests on 256B bytes:
Type of change
Checklist: